<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class PostUserLikesModel extends Model {

    protected $table ="post_user_likes";

    protected $pk = 'id';
    private $cacheKey="post:user:likes:id:";
    private $postIsLikeCache = "user:post:is:like:";
    private $commentIsLikeCache = "user:post:comment:is:like:";
    private $cacheTime=51840000;


    /*
   * 获取total
   * */
    public function getTotal($where=[]){
        return Db::name($this->table)->where($where)->count();
    }


    /*
     * 获取list
     * */
    public function getList($where,$page = null,$limit = null,$orderFields){

        $obj =  Db::name($this->table)->field($this->pk)->where($where);

        if(!empty($field) ) {
            $obj = $obj->field($field);
        }

        if(!empty($page) && !empty($limit)){
            $obj = $obj->page($page, $limit);
        }

        if(!empty($orderFields)){
            $obj = $obj->order($orderFields);
        }

        $list = $obj->select();

        if($list){
            $return = [];
            foreach($list as $item){
                $return[] = $this->getInfo($item[$this->pk]);
            }
            return $return;
        }
        return [];
    }



    /*
    * 获取订单状态详情
    * */
    public function getInfo($id){
        $cacheKey = $this->cacheKey.$id;
        $redisModel = new RedisModel();
        $info = $redisModel->get($cacheKey);
        if($info){
            $info =  json_decode($info,true);
        }else{
            $info = Db::name($this->table)->find($id);
            $info["user"] = (new UserModel())->getUserBaseInfoByUid($info["uid"]);
            $redisModel->set($cacheKey,json_encode($info,256),$this->cacheTime);
        }
        return $info;
    }


    /*
    * 保存数据
    * */
    public function addOne($data){
        return Db::name($this->table)->insertGetId($data);
    }

    /*
    * 修改运单号
    * */
    public function updateOne($id, $data){
        $where[] = [$this->pk,"=",$id];
        return Db::name($this->table)->where($where)->update($data);
    }


    /*
     * 是否已点赞帖子
     * */
    public function isPostLike($uid,$post_id){
        $cacheKey = $this->postIsLikeCache.$uid.":".$post_id;
        $redisModel = new RedisModel();
        $info = $redisModel->get($cacheKey);
        if($info){
            return $info;
        }else{
            $info = $this->getTotal(["uid"=>$uid,"post_id"=>$post_id]);
           if($info > 0){
               $redisModel->set($cacheKey,$info,$this->cacheTime);
           }
        }
        return $info;
    }


    public function delPostLike($uid,$post_id){
        $cacheKey = $this->postIsLikeCache.$uid.":".$post_id;
        $redisModel = new RedisModel();
        return $redisModel->delete($cacheKey);
    }


    /*
   * 是否已点赞帖子
   * */
    public function isPostCommentLike($uid,$comment_id){
        $cacheKey = $this->commentIsLikeCache.$uid.":".$comment_id;
        $redisModel = new RedisModel();
        $info = $redisModel->get($cacheKey);
        if($info){
            $info =  json_decode($info,true);
        }else{
            $info = $this->getTotal(["uid"=>$uid,"comment_id"=>$comment_id]);
            if($info > 0){
                $redisModel->set($cacheKey,$info,$this->cacheTime);
            }
        }
        return $info;
    }


    public function delPostCommentLike($uid,$comment_id){
        $cacheKey = $this->commentIsLikeCache.$uid.":".$comment_id;
        $redisModel = new RedisModel();
        return $redisModel->delete($cacheKey);
    }

}